import com.fasterxml.jackson.databind.ObjectMapper;
import com.stein.pojo.Work;
import com.stein.utils.EsClient;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

public class HomeWork {
    RestHighLevelClient client = EsClient.getClient();

    private String index = "sms-logs-index";



    private ObjectMapper mapper = new ObjectMapper();


    /**
     * 创建目标索引：
     * @throws Exception
     */
    @Test
    public void homeWork() throws Exception{
        //创建分片
        Settings.Builder settings = Settings.builder()
                .put("number_of_shards", 5)
                .put("number_of_replicas", 1);

        XContentBuilder mapping = JsonXContent.contentBuilder()
                .startObject()
                    .startObject("properties")
                        .startObject("createDate")
                            .field("type","date")
                            .field("format","yyyy-MM-dd HH:mm:ss")
                        .endObject()
                        .startObject("sendDate")
                            .field("type","date")
                            .field("format","yyyy-MM-dd HH:mm:ss")
                        .endObject()
                        .startObject("longCode")
                            .field("type","keyword")
                        .endObject()
                        .startObject("mobile")
                            .field("type","keyword")
                        .endObject()
                        .startObject("corpName")
                            .field("type","text")
                            .field("analyzer", "ik_max_word")
                            .field("index", true)
                            .field("store", false)
                        .endObject()
                        .startObject("smsContent")
                            .field("type","text")
                            .field("analyzer", "ik_max_word")
                            .field("index", true)
                            .field("store", false)
                        .endObject()
                        .startObject("state")
                            .field("type","byte")
                        .endObject()
                        .startObject("operatorId")
                            .field("type","byte")
                        .endObject()
                        .startObject("province")
                            .field("type","text")
                            .field("analyzer", "ik_max_word")
                            .field("index", true)
                            .field("store", false)
                        .endObject()
                        .startObject("ipAddr")
                            .field("type","ip")
                        .endObject()
                        .startObject("replyTotal")
                            .field("type","integer")
                        .endObject()
                        .startObject("fee")
                            .field("type","integer")
                        .endObject()
                    .endObject()
                .endObject();

        CreateIndexRequest request = new CreateIndexRequest(index).settings(settings).mapping(mapping);

        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);

        System.out.println("创建的结果是"+response.isAcknowledged());

    }

    /**
     *
     * 测试：添加文档
     */

    @Test
    public  void addDocument() throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse("1999-10-28 00:00:00");

        Work work = new Work("1",date,new Date(),Long.valueOf("16846184684"),Long.valueOf("19953441903"),"啥都卖网络电商","对不起，你咨询的商品我们不卖，虽然我们叫啥都卖！",
                "山东省",0,1,34,66,"192.168.200.129");

        String json = mapper.writeValueAsString(work);

        IndexRequest request = new IndexRequest(index);

        request.id(work.getId()).source(json, XContentType.JSON);

        IndexResponse response = client.index(request, RequestOptions.DEFAULT);

        System.out.println("数据添加结果为："+response.getResult().toString());

    }
}
